{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Python Input, Output and Import.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Python_Input%2C_Output_and_Import.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mL2nLBDWzyTE",
        "colab_type": "text"
      },
      "source": [
        "# Python Input, Output and Import"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "yWtumBRkz1yX",
        "colab_type": "text"
      },
      "source": [
        "## In this tutorial let us understand the Input and Output built-in-functions used in python, also we will learn how to import libraries and use them in our programs.\n",
        "\n",
        "![alt text](https://miro.medium.com/max/1800/1*BC60I6pJU1Xi7wjMPyJUiw.jpeg)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "80iIrL8Iz8-7",
        "colab_type": "text"
      },
      "source": [
        "Before getting started let us understand what are built-in-functions?\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "> Any function that is provided as part of a high-level language and can be executed by a simple [reference](https://www.yourdictionary.com/reference) with or without the specification of arguments — Credits: [Yourdictionary](https://www.yourdictionary.com/built-in-function)\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oPl0tpU-0Sav",
        "colab_type": "text"
      },
      "source": [
        "There are many built-in-functions available in python. It is highly impossible to remember the name and the syntax of every built-in-functions. So below is the documentation of all the built-in-function available in python available in alphabetical order provided by the python.org — **I call it as the cheat sheet of built-in-function, have a look below:**\n",
        "\n",
        "[Python Built-in functions](https://docs.python.org/3/library/functions.html?source=post_page-----73e875516694----------------------)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c2rP8JSF0dWq",
        "colab_type": "text"
      },
      "source": [
        "The `input()` and the `print()` built-in-functions are one of the most commonly used functions for performing standard input and output operations. There is no `output()` function in python, rather `print()` is used to perform the standard output operations."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "p7tJ7iHR6ByJ",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gqDIOrtP1az7",
        "colab_type": "text"
      },
      "source": [
        "# The Output operation using the print function\n",
        "\n",
        "The print function in python is normally used to print the output on the screen. As the name suggests `print` meaning to print something `where?` *on the screen*. Now you don’t have to write the entire body (contents) of the print function every time, personally I don’t even know how the body of the print function looks like. **All you have to do is just call the function by passing the parameters**. Whatever you write inside the print function are called parameters. Let us see how to print something onto the screen."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ih3J7txIABcF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "variable = \"Welcome to Python Tutorials\""
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HqHlZw5sAROQ",
        "colab_type": "code",
        "outputId": "8da6caf5-84bc-428b-a6ca-2a80188c0ca9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "print(variable)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Welcome to Python Tutorials\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7P2Yx-KT171p",
        "colab_type": "text"
      },
      "source": [
        "Like I said the variable is the parameter that is passed along with the print function, you don't need to write the body of the print function you just need to call the print function. **The actual syntax of the print function is given below:**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8pXStLwi2LL9",
        "colab_type": "text"
      },
      "source": [
        "Even you can try this just call **help(print)** and the help function will provide you the help you need (Duh ;))"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-vjCiLKfNM08",
        "colab_type": "code",
        "outputId": "86f5ff97-4bfc-435e-db27-524d6171a49d",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 225
        }
      },
      "source": [
        "help(print)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Help on built-in function print in module builtins:\n",
            "\n",
            "print(...)\n",
            "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
            "    \n",
            "    Prints the values to a stream, or to sys.stdout by default.\n",
            "    Optional keyword arguments:\n",
            "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
            "    sep:   string inserted between values, default a space.\n",
            "    end:   string appended after the last value, default a newline.\n",
            "    flush: whether to forcibly flush the stream.\n",
            "\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gF1u9qtt2SFs",
        "colab_type": "text"
      },
      "source": [
        "Now let us see how to pass more than one parameter inside the print function and what would be the result?"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RCMuxq32Na-6",
        "colab_type": "code",
        "outputId": "3cf522fa-5382-43be-ebd4-63a159edf4f9",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        }
      },
      "source": [
        "variable = \"Welcome to Python Tutorials\"\n",
        "variable2 = \",This is a good place to learn programming\"\n",
        "print(variable, variable2)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Welcome to Python Tutorials ,This is a good place to learn programming\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Rj8geuk22kpp",
        "colab_type": "text"
      },
      "source": [
        "You can pass as many parameters inside the print function but make sure that you separate the variable from one another."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "njQM3xQi2oIH",
        "colab_type": "text"
      },
      "source": [
        "Let us use a “sep” (separator) inside a print function and see the output."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DeSb7AXx20o5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "e1980c0f-4e52-4cc7-a81a-031e9ef8cc24"
      },
      "source": [
        "print(1, 2, 3, 4, 5, sep=\"--->\")"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1--->2--->3--->4--->5\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "y35x1hJf24Mp",
        "colab_type": "text"
      },
      "source": [
        "The “end” parameter is used to append a character to the end of the string which we pass inside the print function."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J8YMmat226LQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "39b5a15e-c5ca-416d-f4f4-7209d08f8670"
      },
      "source": [
        "print(1, 2, 3, 4, 5, end=\" This is the end\")"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1 2 3 4 5 This is the end"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8WMI9fg8286i",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hBOkhTfC29nj",
        "colab_type": "text"
      },
      "source": [
        "## Formatting the output\n",
        "\n",
        "There might be some situations wherein formatting the output is required, in that case, we can use the `str.format()` method."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "08CC7XQo3DUU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "471d50ae-555f-432b-8c2f-4169ce824ef3"
      },
      "source": [
        "variable = \"Python\"\n",
        "variable2 = \"Programming\"\n",
        "print(\"I love {0} and {1}\".format(variable,variable2))"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "I love Python and Programming\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9zKghrqO3Gu-",
        "colab_type": "text"
      },
      "source": [
        "Further, the format() method can do a great job such as adding the comma separator to the string of integers as shown below:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s6432LsK3JNl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "e6069825-51ab-4a8c-9ac3-791550161c47"
      },
      "source": [
        "print(format(1234567, \",d\"))"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1,234,567\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qrJmOnQK3Nnv",
        "colab_type": "text"
      },
      "source": [
        "Similarly, we can format the output using the “`%`” operator as shown below:\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JUCBZRgF3O5L",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "4a0d9fff-b51b-4318-c4ff-1e9bb843d71e"
      },
      "source": [
        "number = 12.3456\n",
        "print(\"The value of x is %1.2f\" %number)"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The value of x is 12.35\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WL-l11J03Rla",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "979e2092-f0e5-4961-e1f8-eac34ab6ae27"
      },
      "source": [
        "print(\"The value of x is %1.5f\" %number)"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The value of x is 12.34560\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JvJAKEki584C",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "NVwpsrGW3VlD",
        "colab_type": "text"
      },
      "source": [
        "# Python Input function\n",
        "\n",
        "In the past, we used to hard code the value of the variable inside our main program. But sometimes we need to take the input from the users. This can be achieved by using the Input function.\n",
        "\n",
        "The Input function as the name suggests taking the user's input. The syntax of the input function is:\n",
        "\n",
        "`input(prompt = ‘ ’)`"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "rykNWJwA3_CC",
        "colab_type": "text"
      },
      "source": [
        "Here the prompt is the one that would be displayed on the screen."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JtkMy-sn4A8u",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 52
        },
        "outputId": "0190f38d-e81c-4958-d013-07ec3b619783"
      },
      "source": [
        "number = input(\"Enter the number of your choice: \")\n",
        "print(\"The number that you have entered is: %s\" %number)"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Enter the number of your choice: 100\n",
            "The number that you have entered is: 100\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "k_m_gDtp4GF8",
        "colab_type": "text"
      },
      "source": [
        "Here, the prompt is “Enter the number of your choice”, here you might wonder why did I use “%s” when I entered an integer as an input. It is because all the variables are stored as string in Python, to explicitly typecast it use can use either int(variable_name), or float(variable_name) and many more. Go try it and let me know.\n",
        "\n",
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "f6A1eVD04KOC",
        "colab_type": "text"
      },
      "source": [
        "# Python Import\n",
        "\n",
        "Sometimes, we need to import some libraries from a different module (a module is a python file that comprises definitions and statements). In those cases, we can import those methods or modules by using the import keyword along with the name of the module.\n",
        "\n",
        "Consider an example, think you want to see what is the value of “pi”, all you can do is rather than doing (22/7, or memorizing 3.1423… I don't know further), just import the math module and call the math method and then your job is done.\n"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dIreuh5q4mWG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "92bbfd31-d576-407c-db55-d2ee1b91dd68"
      },
      "source": [
        "import math\n",
        "print(math.pi)"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3.141592653589793\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IW0_nlY55pSb",
        "colab_type": "text"
      },
      "source": [
        "Also using the from keyword we can access specific attributes from the module. For example:"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gVSUdWZG5wnq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "outputId": "389b175f-08b4-4c20-942c-88e94c8a3424"
      },
      "source": [
        "from math import pi\n",
        "print(pi)"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3.141592653589793\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "uMXl-z-V5yiK",
        "colab_type": "text"
      },
      "source": [
        "It's more like “From this module import this function, method or attribute”.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "A752LlbU50JF",
        "colab_type": "text"
      },
      "source": [
        "This is the end of the “Python Input, Output and Import” tutorial. Hope you enjoyed it. If you have any comments or suggestions let me know in the comment section below. Until then Goodbye !!!."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "e9P-oMP450ss",
        "colab_type": "text"
      },
      "source": [
        "\n",
        "\n",
        "---\n",
        "\n"
      ]
    }
  ]
}